package com.bub.pay.mq.rocketmq.producer;

import com.aliyun.openservices.ons.api.*;
import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter;
import com.aliyun.openservices.ons.api.transaction.TransactionProducer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class BNMQProducer {

    /**
     * 阿里云版rocketmq 发送端同步(同步消息发送默认重试2次一共3次) 异步发送模式都不会丢消息(异步消息发送没有重试)
     * Oneway模式发送会丢失
     */
    @Resource
    private Producer onsProducer;

    @Resource
    private TransactionProducer transactionProducer;


    //    Message message = new Message(TP_AcceptPayNotify, null, key.toString(), JSON.toJSONBytes(paymentRequest));

    /**
     * 同步发送
     *
     * @param message
     * @return
     */
    public SendResult sendMQMessage(Message message) {
        return onsProducer.send(message);
    }

    /**
     * 异步发送
     *
     * @param message
     */
    public void sendMQMessageAsync(Message message) {
        onsProducer.sendAsync(message, new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {

            }

            @Override
            public void onException(OnExceptionContext onExceptionContext) {

//                log.error("接受通知支付成功_消息发送失败 进行本地持久化 错误:{}", onExceptionContext.getException().getMessage());

            }
        });
    }

    /**
     * 事务消息
     *
     * @param message
     * @param localTransactionExecuter
     * @param arg
     * @return
     */
    public SendResult sendTransactionMessage(Message message, LocalTransactionExecuter localTransactionExecuter, Object arg) {
        return transactionProducer.send(message, localTransactionExecuter, arg);
    }
}
